﻿/* ============================================================================
(c) Copyright 2012 Hewlett-Packard Development Company, L.P.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge,publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
============================================================================ */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HPCloud.Test;
using HPCloud.Objects.DataAccess;
using HPCloud.Objects.Domain;
using HPCloud.Objects.Domain.Admin;

namespace HPCloud.Objects.Test.Unit_Test.ObjectStorage
{
    public class StorageTest : BaseUnitTest
    {
//===========================================================================================================
/// <summary>
/// 
/// </summary>
//===========================================================================================================
        private string TestStorageContainer
        {
            get
            {
                return this.Context.Settings.TeststorageContainer;
            }
        }
//===========================================================================================================
/// <summary>
/// 
/// </summary>
//==========================================================================================================
        protected string StorageURL
        {
            get
            {
                return this.Context.ServiceCatalog.GetService(Services.ObjectStorage).Url;
            }
        }
//===========================================================================================================
/// <summary>
/// A. Recreates the test storageContainer (B2).
/// B. Changes the current Drive Location to b2 (the root).
/// C. Creates D1 folder adjacent to root
/// D. Creates D2 folder adjacent to D1
/// E. Copies our test file (Anothertest.txt) to a specified Server location. Path must be fully qualified.
/// </summary>
//===========================================================================================================>
        protected void ResetTestData(string copyTestFileTargetPath)
        {
            this.ResetTestData();

            // Get our target and source paths in order..

            string targetPath = copyTestFileTargetPath;
            string sourcePath = this.Context.Settings.TestFilePath;

            // Do the copy..

            IStorageObjectRepository repository = this.Factory.CreateStorageObjectRepository();
            repository.Copy(sourcePath, targetPath, false);            
        }
//===========================================================================================================
/// <summary>
/// A. Recreates the test storageContainer (B2).
/// B. Changes the current Drive Location to b2 (the root).
/// C. Creates D1 folder adjacent to root
/// D. Creates D2 folder adjacent to D1
/// E. Optionally copies our test file (Anothertest.txt) to root.
/// </summary>
//===========================================================================================================>
        protected void ResetTestData(bool includeTestFileInRoot)
        {
            this.ResetTestData();

            if (includeTestFileInRoot)
            {
                // Get our target and source paths in order..

                string endpointURL = this.Context.ServiceCatalog.GetService(Services.ObjectStorage).Url;
                StoragePath sourcePath = new StoragePath(this.Context.Settings.TestFilePath);
                StoragePath targetPath = new StoragePath(endpointURL + "/testcon/Anothertest.txt");

                // Load the local file up and copy it over to the server..

                IStorageObjectRepository repository = this.Factory.CreateStorageObjectRepository();
                StorageObject storageObject  = new StorageObject();

                storageObject.LoadFile(this.Context.Settings.TestFilePath);
                storageObject.Key = targetPath.ResourcePath;
                repository.Copy(storageObject, targetPath.AbsoluteURI, true);
            }
        }
//===========================================================================================================>
/// <summary>
/// A. Recreates the test storageContainer (B2).
/// B. Changes the current Drive Location to b2 (the root).
/// C. Creates D1 folder adjacent to root
/// D. Creates D2 folder adjacent to D1
/// </summary>
//===========================================================================================================>
        protected void ResetTestData()
        {
            IContainerRepository storageContainerRepository = this.Factory.CreateContainerRepository();
            IStorageObjectRepository objectRepository       = this.Factory.CreateStorageObjectRepository();

            // Destroy the old test storageContainer (and its contents), then create a new one from scratch..

            storageContainerRepository.DeleteContainer(this.TestStorageContainer);
            storageContainerRepository.SaveContainer(new StorageContainer(this.TestStorageContainer));

            // Create a few test folders to work with...            

            string storageManagementUrl = this.Context.ServiceCatalog.GetService("object-store").Url;

            objectRepository.MakeFolder(storageManagementUrl + "/" + this.TestStorageContainer + @"\d1\");
            objectRepository.MakeFolder(storageManagementUrl + "/" + this.TestStorageContainer + @"\d1\d2\");
        }
//===========================================================================================================>
/// <summary>
/// A. Recreates the test storageContainer (B3).
/// B. Changes the current Drive Location to b3 (the root).
/// C. Creates D1 folder adjacent to root
/// D. Creates D2 folder adjacent to D1
/// </summary>
//===========================================================================================================>
        //public void ResetSecondarystorageContainer()
        //{
        //    IContainerRepository storageContainerRepository = this.RepositoryFactory.CreateContainerRepository();
        //    IStorageObjectRepository objectRepository = this.RepositoryFactory.CreateStorageObjectRepository();

        //    // Destroy the old test storageContainer (and its contents), then create a new one from scratch..

        //    try
        //    {
        //        storageContainerRepository.DeleteContainer(this.TeststorageContainerSecondary);
        //    }
        //    catch (Exception)
        //    {
        //        // Eat this if the storageContainer we're file not found.. Http considers this an Exception, we assume this is an acceptable situation.
        //    };

        //    storageContainerRepository.SaveContainer(new StorageContainer(this.TeststorageContainerSecondary));

        //    // Bind to the new drive\storageContainer. Subsequent calls to this.ExecutePipelineCommand should now operate against the KVS Provider..

        //    this.ExecutePipelineCommand("cd " + this.TeststorageContainerSecondary + ":");

        //    // Create a few test folders to work with...

        //    objectRepository.MakeFolder(this.TeststorageContainerSecondary + @"\d1");
        //    objectRepository.MakeFolder(this.TeststorageContainerSecondary + @"\d1\d2");
        //}
    }
}
